package ui;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import each_class.User;
import ui.global.GraphicsUtil;

public class LoginUI extends AbstractUI {
	
	private Button regist;
	private Button login;
	private TextBar comfirPpassWord;
	private boolean isRegist;
	private TextBar acount;
	private TextBar passWord;
	private Label exceptionView;
	private User user;
	private Image avatarImage;
	private Label tileRegist;
	private Button editAvatar;
	private Composite avatar;
	private String path;

	public LoginUI (Composite parent) {
		super(parent);
		title.setText("登录");
		
		tileRegist = new Label(this, SWT.NONE);
		tileRegist.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		tileRegist.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 29, SWT.BOLD));
		tileRegist.setBounds(10, 16, 215, 55);
		tileRegist.setText("注册");
		tileRegist.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 23, SWT.BOLD));
		tileRegist.setLocation(-215, 18);
		
		avatar = new Composite(this, SWT.NONE);
		avatar.setRegion(ui.global.GraphicsUtil.createRoundedRectangle(0, 0, 0, 0, 64));
		avatar.setBackground(ui.global.UIColor.PUREWHITE);
		avatar.setBackgroundImage(SWTResourceManager.getImage("src/image/no_picture_x128.png"));
		avatar.setBounds(541, 60, 128, 128);
		
		editAvatar = new Button(avatar, SWT.NONE) {
			
			@Override
			public void mouseUp(MouseEvent e) {
				if (isRegist) {
					FileDialog fileDialog = new FileDialog(getShell());
					fileDialog.setFilterNames(new String[] {"图片"});
					fileDialog.setFilterExtensions(new String[] {"*.jpg;*.jpeg;*.png"});
					fileDialog.setText("请选择头像");
					fileDialog.setFilterPath("SystemDrive");
					path = fileDialog.open();
					editAvatar.setVisible(isRegist);
					if (path != null) {
						LoginUI.this.avatarImage = new Image(null, path);
						avatar.setBackgroundImage(GraphicsUtil.resize(LoginUI.this.avatarImage, 128, 128));
					}
				}
			}
		};
		editAvatar.initial(
				SWTResourceManager.getImage("src/image/edit_deactive_x64.png"),
				SWTResourceManager.getImage("src/image/edit_active_x64.png"));
		editAvatar.setBounds(32, 32, 64, 64);
		editAvatar.setRegion(ui.global.GraphicsUtil.handleTransparenceRegion(SWTResourceManager.getImage("src/image/edit_active_x64.png"), 64, 64));
		editAvatar.setVisible(false);
		avatar.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				if(e.x > 32 && e.y > 32 && e.x < 96 && e.y < 96 ) {
					return;
				}
				editAvatar.setVisible(true);
				GC gc = new GC(avatar);
				gc.setAlpha(100);
				gc.setBackground(ui.global.UIColor.BLACK);
				gc.setLineWidth(128);
				gc.drawLine(0, 64, 128, 64);
				gc.dispose();
			}
			
			@Override
			public void mouseExit(MouseEvent e) {
				if (e.x > 32 && e.y > 32 && e.x < 96 && e.y < 96 ) {
					return;
				}
				editAvatar.setVisible(false);
				avatar.redraw();
			}
		});
		
		acount = new TextBar(this, SWT.NONE);
		acount.initail(SWTResourceManager.getImage("src/image/login_textBar_background.png"), 270, 40);
		acount.setBounds(470, 260, 270, 40);
		acount.setBackground(ui.global.UIColor.GRAY);
		acount.setMessage("账号");

		passWord = new TextBar(this, SWT.PASSWORD);
		passWord.initail(SWTResourceManager.getImage("src/image/login_textBar_background.png"), 270, 40);
		passWord.setBounds(470, 320, 270, 40);
		passWord.setBackground(ui.global.UIColor.GRAY);
		passWord.setMessage("密码");

		comfirPpassWord = new TextBar(this, SWT.PASSWORD);
		comfirPpassWord.initail(SWTResourceManager.getImage("src/image/login_textBar_background.png"), 270, 40);
		comfirPpassWord.setBounds(470, 320, 270, 40);
		comfirPpassWord.setBackground(ui.global.UIColor.GRAY);
		comfirPpassWord.setMessage("确认密码");
		comfirPpassWord.setVisible(false);
		

		login = new Button(this, SWT.NONE) {

			@Override
			public void mouseUp(MouseEvent e) {
				String acount = LoginUI.this.acount.getText();
				String password = LoginUI.this.passWord.getText();
				
				
				
				if (acount.equals("")) {
					showException("请输入账号！");
					return;	
				}
				
				if (password.equals("")) {
					showException("请输入密码！");
					return;	
				}

				if (isRegist) {
					String comfirPpassWord = LoginUI.this.comfirPpassWord.getText();
					if (comfirPpassWord.equals("") || password.equals("")
							|| !comfirPpassWord.equals(password)) {
						showException("密码不一致！");
						return;
					}
					new Thread() {
						public void run() {
							getDisplay().syncExec(new Runnable() {

								@Override
								public void run() {
									LoginUI.this.user = ui.global.Operation.regist(acount, password, path);
								}
							});
							if (LoginUI.this.user != null) {
								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {
										LoginUI.this.exceptionView.setText("注册成功!您的账号是：" + user.getUser_id());
										LoginUI.this.exceptionView.setVisible(true);
									}
								});
								try {
									Thread.sleep(2000L);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {
										ui.global.Current.getPlaySationButton().active();
									}
								});
								ui.global.Current.setCurrentUser(user);
							} else {
								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {
										showException("注册失败！");
									}
								});
							}
						};
					}.start();
				} else {
					new Thread() {
						public void run() {
							getDisplay().syncExec(new Runnable() {
								@Override
								public void run() {
									if (acount.equals("")) {
										showException("请输入账号");
										return;
									}
									if (password.equals("")) {
										showException("请输入密码！");
										return;
									}
								}
							});

							try {

								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {

									}
								});
								LoginUI.this.user = ui.global.Operation.login(acount, password);
							} catch (IOException e) {
								e.printStackTrace();
							}
							if (LoginUI.this.user != null) {
								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {
										LoginUI.this.exceptionView.setText("登录成功！");
										LoginUI.this.exceptionView.setVisible(true);
									}
								});
								try {
									Thread.sleep(1000L);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {
										ui.global.Current.getPlaySationButton().active();
									}
								});
								ui.global.Current.setCurrentUser(LoginUI.this.user);
							} else {
								getDisplay().syncExec(new Runnable() {
									@Override
									public void run() {
										showException("登录失败！");
									}
								});
							}
						};
					}.start();
				}
			}
		};

//		login.setRegion(ui.global.GraphicsUtil.handleTransparenceRegion(
//				SWTResourceManager.getImage("src/image/login_button_x64_deactive.png"), 64, 64));
		login.initial(SWTResourceManager.getImage("src/image/login_button_x64_deactive.png"),
				SWTResourceManager.getImage("src/image/login_button_x64_active.png"));
		login.setBounds(641, 403, 64, 64);

		regist = new Button(this, SWT.NONE) {

			@Override
			public void mouseUp(MouseEvent e) {
				loadRegistPage();
				isRegist = true;
			}
		};

//		regist.setRegion(ui.global.GraphicsUtil.handleTransparenceRegion(
//				SWTResourceManager.getImage("src/image/regist_button_x64_deactive.png"), 64, 64));
		regist.initial(SWTResourceManager.getImage("src/image/regist_button_x64_deactive.png"),
				SWTResourceManager.getImage("src/image/regist_button_x64_active.png"));
		regist.setBounds(501, 400, 64, 64);

		exceptionView = new Label(this, SWT.CENTER);
		exceptionView.setBounds(0, 604, 1210, 32);
		exceptionView.setForeground(ui.global.UIColor.RED);
		exceptionView.setVisible(false);
	}

	@Override
	public void active() {
		setVisible(true);
	}

	private void initail(){
		isRegist = false;
		title.setLocation(35, 18);
		try {
			tileRegist.setLocation(-215, 18);
			avatar.setBackgroundImage(SWTResourceManager.getImage("src/image/no_picture_x128.png"));
			acount.setText("");
			acount.setMessage("账号");
			passWord.setText("");
			comfirPpassWord.setBounds(470, 320, 270, 40);
			comfirPpassWord.setText("");
			comfirPpassWord.setVisible(false);
			login.setBounds(641, 403, 64, 64);
			regist.setBounds(501, 400, 64, 64);
			regist.setVisible(true);
		} catch (NullPointerException e) {
		}
	}
	
	@Override
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		if (!visible) {
			initail();
		}
	}
	
	private void showException(String exception) {
		exceptionView.setText(exception);
		exceptionView.setVisible(true);
		new Thread() {

			public void run() {

				try {
					Thread.sleep(1000l);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				getDisplay().asyncExec(new Runnable() {

					@Override
					public void run() {
						exceptionView.setVisible(false);
					}
				});

			};
		}.start();
	}

	private void loadRegistPage() {
		acount.setText("");
		acount.setMessage("昵称");
		regist.setVisible(false);
		new Thread() {
			public void run() {
				for (int i = 0; i < 10; i++) {
					getDisplay().asyncExec(new Runnable() {

						@Override
						public void run() {
							title.setLocation(title.getLocation().x - 25, 18);
							login.setLocation(login.getLocation().x - 7, 403);
						}
					});
					try {
						Thread.sleep(20l);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

				getDisplay().asyncExec(new Runnable() {

					@Override
					public void run() {
						comfirPpassWord.setVisible(true);
					}
				});

				for (int i = 0; i < 10; i++) {
					getDisplay().asyncExec(new Runnable() {

						@Override
						public void run() {
							tileRegist.setLocation(tileRegist.getLocation().x + 25, 18);
							login.setLocation(571, login.getLocation().y + 6);
							comfirPpassWord.setLocation(470, comfirPpassWord.getLocation().y + 6);
						}
					});
					try {
						Thread.sleep(20l);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
		}.start();
	}

}
